package com.ecgsystem.activity;

import java.util.concurrent.ConcurrentLinkedQueue;

import com.ecgsystem.panel.Panel6;

import com.ecgsystem.activity.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.FrameLayout.LayoutParams;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

/**
 * TODO Put here a description of what this class does.
 *
 * @author ANHTUAN.
 *         Created Sep 24, 2012.
 */
public class StoredSignal extends Activity implements OnSeekBarChangeListener, Runnable{
	private static Panel6 panel;
	private static SeekBar seekBar;
	private LinearLayout.LayoutParams panelParam;
	private LinearLayout.LayoutParams seekBarParams;
	private ScrollView verticalView;
	private HorizontalScrollView horizotalView;
	private LinearLayout mainLayout;
	private LinearLayout internalWrapper;
	/**
	 * TODO Put here a description of this field.
	 */
	public static boolean startDraw = false;
	/**
	 * TODO Put here a description of this field.
	 */
	public static ConcurrentLinkedQueue<Float> QueueDraw[];
	/**
	 * TODO Put here a description of this field.
	 */
	public static Context storedSignalContex;
	private static int loadedPercent;// phan tram ban ghi da load duoc
	private Thread thread;
	private ProgressDialog waitDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		storedSignalContex = this;

		this.mainLayout = new LinearLayout(this);
		this.mainLayout.setOrientation(LinearLayout.VERTICAL);
		// cuon doc
		this.verticalView = new ScrollView(this);
		// cuon ngang
		this.horizotalView = new HorizontalScrollView(this);

		seekBar = new SeekBar(this);
		this.seekBarParams = new LinearLayout.LayoutParams(300,
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		seekBar.setLayoutParams(this.seekBarParams);

		this.panelParam = new LinearLayout.LayoutParams(
				android.view.ViewGroup.LayoutParams.FILL_PARENT,
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		this.panelParam.height = 525;
		this.panelParam.width = 1200;

		panel = new Panel6(this);
		panel.setLayoutParams(this.panelParam);
		panel.clearFocus();
		panel.setEnabled(false);
		// Tao man hinh cuon ngang
		this.internalWrapper = new LinearLayout(this);
		this.internalWrapper.setLayoutParams(new LayoutParams(
				android.view.ViewGroup.LayoutParams.FILL_PARENT,
				android.view.ViewGroup.LayoutParams.FILL_PARENT));

		this.horizotalView.addView(this.internalWrapper);

		this.internalWrapper.addView(panel);
		// chuyen dong mem mai
		this.horizotalView.setSmoothScrollingEnabled(true);
		// Tao man hinh cuon doc
		this.verticalView.addView(this.horizotalView, new ViewGroup.LayoutParams(
				android.view.ViewGroup.LayoutParams.FILL_PARENT,
				android.view.ViewGroup.LayoutParams.FILL_PARENT));
		this.verticalView.setSmoothScrollingEnabled(true);
		this.verticalView.setBackgroundResource(R.drawable.ecg_background);

		this.mainLayout.addView(seekBar);
		this.mainLayout.addView(this.verticalView);
		setContentView(this.mainLayout);

		loadedPercent = 0;
		setLoadedPercent(loadedPercent);
		seekBar.setOnSeekBarChangeListener(this);
		panel.setQueueDraw(QueueDraw);
	}

	@Override
	public void onStart() {
		super.onStart();
		this.thread = new Thread(this);
		showWaitDialog();
		this.thread.start();
	}

	private void showWaitDialog() {
		this.waitDialog = ProgressDialog.show(this, "", "Dang tai ban ghi ...",
				true, true);
	}
	
	private void deleteWaitDialog() {
		if (this.waitDialog != null) {
			this.waitDialog.dismiss();
		}
	}
	
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			deleteWaitDialog();
		}
	};

	@Override
	public void onResume() {
		super.onResume();
	}
	
	/**
	 * TODO Put here a description of what this method does.
	 *
	 */
	public void StopDraw() {
		startDraw = false;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		super.onKeyDown(keyCode, event);
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			startDraw = false;
			PatientInfo.stopGetSignal();
		}
		return true;
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		if(progress > loadedPercent){
			seekBar.setProgress(loadedPercent);
			progress = loadedPercent;
		}
		panel.startDrawAt(progress);
	}

	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub

	}

	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub

	}
	
	/**
	 * TODO Put here a description of what this method does.
	 *
	 * @param diff
	 */
	public static void incrementLoadedPercent(final int diff){
		seekBar.incrementSecondaryProgressBy(diff);
		loadedPercent += diff;
	}

	/**
	 * TODO Put here a description of what this method does.
	 *
	 * @param i
	 */
	public static void setLoadedPercent(final int i) {
		loadedPercent = i;
		seekBar.setSecondaryProgress(i);
	}

	public void run() {
		while(!startDraw){
			Log.i("drawing","ok");
		}
		this.handler.sendEmptyMessage(0);
	}
}
